﻿using System;
using System.Collections;
using System.Drawing;
using System.Threading;
using Console = Colorful.Console;

namespace FlyingChess
{
    internal class Program
    {
        private static int[] chessNum = new int[66]; //总格子数

        private static int[] fiveStar = { 3, 5, 12, 19, 24, 29, 36, 43, 48, 57, 64 }; //地雷
        private static int[] corona = { 2, 16, 26, 32, 41, 61 }; //幸运轮盘
        private static int[] stop = { 9, 39, 53 }; //暂停
        private static int[] tunnel = { 21, 55 }; //时空隧道

        private static void Main(string[] args)
        {
            #region 一些必要的变量定义
            int dice = 0; //存放掷骰子后的数字
            string nameA = ""; //玩家A的昵称
            string nameB = ""; //玩家B的昵称
            int playPosA = 0; //玩家A坐标
            int playPosB = 0; //玩家B坐标
            bool stopA = false; //是否踩到暂停
            bool stopB = false;
            Random rd = new Random();
            #endregion

            #region 玩家A的输入

            Console.WriteLine("请输入玩家A的姓名：");
            nameA = Console.ReadLine();
            while (string.IsNullOrEmpty(nameA))
            {
                Console.WriteLine("玩家姓名不能为空,请重新输入");
                nameA = Console.ReadLine();
            }

            #endregion 玩家A的输入

            #region 玩家B的输入

            Console.WriteLine("请输入玩家B的姓名：");
            nameB = Console.ReadLine();
            while (string.IsNullOrEmpty(nameB) || nameB == nameA)
            {
                if (string.IsNullOrEmpty(nameB))
                {
                    Console.WriteLine("玩家姓名不能为空,请重新输入");
                    nameB = Console.ReadLine();
                }
                else
                {
                    Console.WriteLine("玩家A与玩家B姓名相同，请重新输入");
                    nameB = Console.ReadLine();
                }
            }

            #endregion 玩家B的输入

            Console.Clear();

            #region 游戏逻辑

            while (playPosA < 65 && playPosB < 65)
            {
                GameTips(nameA, nameB);

                ConsoleChess(playPosA, playPosB);
                if (!stopA)
                {
                    Console.WriteLine("\r\n");
                    Console.WriteLine("玩家{0}请按任意键开始掷骰子", nameA);
                    Console.ReadKey(true);
                    dice = rd.Next(1, 7);
                    Console.WriteLine("{0}掷出了：{1}", nameA, dice);
                    Console.ReadKey(true);
                    Console.WriteLine("{0}开始行动...", nameA);
                    Thread.Sleep(500);
                    playPosA += dice;
                    Console.WriteLine("{0}行动完了,按任意键继续", nameA);
                    Console.ReadKey(true);

                    GameLogic(ref playPosA, ref playPosB, out string message, out bool chenge, out stopA, out stopB);
                    Console.WriteLine(message);
                    Console.ReadKey(true);
                    if (chenge)
                    {
                        while (int.TryParse(Console.ReadLine(), out int num))
                        {
                            if (num == 1)
                            {
                                int tmpPos = playPosA;
                                playPosA = playPosB;
                                playPosB = tmpPos;
                                break;
                            }
                            else if (num == 2)
                            {
                                playPosB -= 6;
                                break;
                            }
                            else
                            {
                                Console.WriteLine("请输入正确的数");
                            }
                        }
                    }
                    else
                    {
                        chenge = false;
                    }
                }
                else
                {
                    stopA
                        
                        = false;
                }
                Console.Clear();
                GameTips(nameA, nameB);
                ConsoleChess(playPosA, playPosB);
                if (!stopB)
                {
                    Console.WriteLine("\r\n");
                    Console.WriteLine("玩家{0}请按任意键开始掷骰子", nameB);
                    Console.ReadKey(true);
                    dice = rd.Next(1, 7);
                    Console.WriteLine("{0}掷出了：{1}", nameB, dice);
                    Console.ReadKey(true);
                    Console.WriteLine("{0}开始行动...", nameB);
                    Thread.Sleep(500);
                    playPosB += dice;
                    Console.WriteLine("{0}行动完了,按任意键继续", nameB);
                    Console.ReadKey(true);

                    GameLogic(ref playPosA, ref playPosB, out string message, out bool chenge, out stopA, out stopB);
                    Console.WriteLine(message);
                    Console.ReadKey(true);
                    if (chenge)
                    {
                        while (int.TryParse(Console.ReadLine(), out int num))
                        {
                            if (num == 1)
                            {
                                int tmpPos = playPosB;
                                playPosB = playPosA;
                                playPosA = tmpPos;
                                break;
                            }
                            else if (num == 2)
                            {
                                playPosA -= 6;
                                break;
                            }
                            else
                            {
                                Console.WriteLine("请输入正确的数");
                            }
                        }
                    }
                    chenge = false;

                }
                else
                {
                    stopB = false;
                }
                Console.Clear();
            }

            #endregion 游戏逻辑

            #region 游戏胜利
            Console.WriteLine("游戏结束！！！！！");
            if (playPosA >= 65)
            {
                Console.WriteLine("玩家：{0} 胜利！！！", nameA);
            }
            else
            {
                Console.WriteLine("玩家：{0} 胜利！！！", nameB);
            }
            #endregion

            Console.ReadLine();
        }

        /// <summary>
        /// 游戏提示
        /// </summary>
        /// <param name="nameA"></param>
        /// <param name="nameB"></param>
        private static void GameTips(string nameA, string nameB)
        {
            Console.WriteLine(nameA + "的人物用A表示");
            Console.WriteLine(nameB + "的人物用B表示");
            Console.WriteLine("图例:幸运轮盘:◎   地雷:☆   暂停:▲   时空隧道:卐");
        }

        /// <summary>
        /// 根据步数判断是否踩中对应格子
        /// </summary>
        public static void GameLogic(ref int posA, ref int posB, out string msg, out bool chenge, out bool stopA, out bool stopB)
        {
            msg = "";
            chenge = false;
            stopA = false;
            stopB = false;

            //踩到地雷
            for (int i = 0; i < fiveStar.Length; i++)
            {
                if (posA == fiveStar[i])
                {
                    msg = "该玩家踩到地雷，倒退6格";
                    posA -= 6;
                    if (posA < 0)
                    {
                        posA = 0;
                    }
                }
                if (posB == fiveStar[i])
                {
                    msg = "该玩家踩到地雷，倒退6格";
                    posB -= 6;
                    if (posB < 0)
                    {
                        posB = 0;
                    }
                }
            }
            //踩到幸运轮盘
            for (int i = 0; i < corona.Length; i++)
            {
                if (posA == corona[i] || posB == corona[i])
                {
                    msg = "该玩家踩到幸运轮盘，请选择：1-交换位置  2-轰炸对方(轰炸将使对方倒退6格)";
                    chenge = true;
                }
            }
            //踩到暂停
            for (int i = 0; i < stop.Length; i++)
            {
                if (posA == stop[i] || posB == stop[i])
                {
                    msg = "该玩家踩到暂停，将暂停一回合";
                    stopA = true;
                }
                if (posB == stop[i])
                {
                    msg = "该玩家踩到暂停，将暂停一回合";
                    stopB = true;
                }
            }
            //踩到时空隧道
            for (int i = 0; i < tunnel.Length; i++)
            {
                if (posA == tunnel[i])
                {
                    msg = "该玩家踩到时空隧道，前进10格";
                    posA += 10;
                }
                if (posB == tunnel[i])
                {
                    msg = "该玩家踩到时空隧道，前进10格";
                    posB += 10;
                }
            }
        }

        /// <summary>
        /// 初始化地图
        /// </summary>
        public static void ConsoleChess(int posA, int posB)
        {
            int num = 0; //记录格子数

            ArrayList arr = new ArrayList();
            //赋值（格子数）
            for (int i = 0; i < chessNum.Length; i++)
            {
                chessNum[i] = i;
            }

            #region 打印地图

            //竖1
            for (int i = 0; i < 5; i++)
            {
                if (posA == posB && num == 0)
                {
                    Console.WriteLine("<>");
                }
                else
                {
                    Console.WriteLine(ChessType(chessNum[i], posA, posB));
                }
                num++;
            }
            //横1
            for (int i = 0; i < 17; i++)
            {
                Console.Write(ChessType(chessNum[num], posA, posB));
                num++;
            }
            Console.WriteLine();
            //竖2
            for (int i = 0; i < 5; i++)
            {
                Console.WriteLine("\t\t\t\t" + ChessType(chessNum[num], posA, posB));
                num++;
            }
            //横2
            for (int i = 44; i > 27; i--)
            {
                Console.Write(ChessType(chessNum[i], posA, posB));
                num++;
            }
            Console.WriteLine();
            //竖3
            for (int i = 0; i < 5; i++)
            {
                Console.WriteLine(ChessType(chessNum[num], posA, posB));
                num++;
            }
            //横3
            for (int i = 0; i < 17; i++)
            {
                Console.Write(ChessType(chessNum[num], posA, posB));
                num++;
            }

            #endregion 打印地图
        }

        /// <summary>
        /// 根据格数返回不同的格子
        /// </summary>
        /// <param name="num"></param>
        public static string ChessType(int num, int a, int b)
        {
            //地雷
            //int[] fiveStar = { 3, 5, 12, 19, 24, 29, 36, 43, 48, 57, 64 };
            for (int i = 0; i < fiveStar.Length; i++)
            {
                if (num == fiveStar[i])
                {
                    Console.ForegroundColor = Color.Green;
                    return "☆";
                }
            }
            //幸运轮盘
            //int[] corona = { 2, 16, 26, 32, 41, 61 };
            for (int i = 0; i < corona.Length; i++)
            {
                if (num == corona[i])
                {
                    Console.ForegroundColor = Color.Blue;
                    return "◎";
                }
            }
            //暂停
            //int[] stop = { 9, 39, 53 };
            for (int i = 0; i < stop.Length; i++)
            {
                if (num == stop[i])
                {
                    Console.ForegroundColor = Color.Yellow;
                    return "▲";
                }
            }
            //时空隧道
            //int[] tunnel = { 21, 55 };
            for (int i = 0; i < tunnel.Length; i++)
            {
                if (num == tunnel[i])
                {
                    Console.ForegroundColor = Color.Magenta;
                    return "卐";
                }
            }
            //玩家
            if (num == a)
            {
                Console.ForegroundColor = Color.FromArgb(250, 140, 53);
                return "A";
            }
            if (num == b)
            {
                Console.ForegroundColor = Color.FromArgb(51, 163, 220);
                return "B";
            }
            Console.ResetColor();
            return "口";
        }

    }
}